package generator;

import freemarker.template.Template;
import generator.util.MyFileUtils;
import generator.util.FreeMarkerHelp;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-8-9
 * Time: 下午12:09
 * To change this template use File | Settings | File Templates.
 */
public class Generator {
    public static String dir = System.getProperty("user.dir");
    public static String templateDirPath = Generator.class.getResource("").getPath();
    public static String hardDisk ="E:/";
    public static String projectName="generteProject";
    public static String path=projectName+"/src/main/";


    public static void process() throws Exception {
        BeanGenerator.class.newInstance().generate("bean", "com.easy.yours.biz.model", "generator.model");
        DaoGenerator.class.newInstance().generate("dao", "com.easy.yours.biz.dao", "generator.model");
        DaoImplGenerator.class.newInstance().generate("daoImpl", "com.easy.yours.biz.dao.impl", "generator.model");
        ServiceGenerator.class.newInstance().generate("service", "com.easy.yours.biz.service", "generator.model");
        ServiceImplGenerator.class.newInstance().generate("serviceImpl", "com.easy.yours.biz.service.impl", "generator.model");
        ControllerGenerator.class.newInstance().generate("controller", "com.easy.yours.biz.controller", "generator.model");
        XmlConfigGenerator.class.newInstance().generate("applicationContext", "applicationContext", "com.easy.yours.biz.model");
        XmlConfigGenerator.class.newInstance().generate("hibernate.cfg", "hibernate.cfg", "com.easy.yours.biz.model");
        Generator.generateWebCommon(dir + "/web", "E:/generteProject/src/main/webapp");
        WebGenerator.class.newInstance().generateWebJs("com.easy.yours.biz.model", "generator.model");
        WebGenerator.class.newInstance().generateWebJsp("generator.model");
        PomGenerator.class.newInstance().generate("mygeneratorproject");
    }

    public void generate(Map<String, Object> root, String templateName, String fileName, String packageName) throws Exception {
//        File templateDir = MyFileUtils.makeDir("E:/", "project/branches/generator/src/generator/template");
        File templateDir = new File(templateDirPath + "/template/");

        //定义输出
        File outputDir = MyFileUtils.makeDir(hardDisk, path+"java/" + packageName.replace(".", "/"));
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputDir + "/" + fileName + ".java");
        template.process(root, out);
        out.flush();
        out.close();
    }

    public void generateXml(Map<String, Object> root, String templateName, String fileName, String packageName) throws Exception {
//        File templateDir = MyFileUtils.makeDir("E:/", "project/branches/generator/src/generator/template");
        File templateDir = new File(templateDirPath + "/template/");

        //定义输出
        File outputDir = MyFileUtils.makeDir(hardDisk, path+"resources");
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputDir + "/" + fileName + ".xml");
        template.process(root, out);
        out.flush();
        out.close();
    }

    public static void generateWebCommon(String sourcePath, String desPath) throws Exception {
        FileUtils.copyDirectory(new File(sourcePath), new File(desPath));
    }

    public void generateWebJs(Map<String, Object> root, String templateName, String fileName, String className) throws Exception {
//        File templateDir = MyFileUtils.makeDir("E:/", "project/branches/generator/src/generator/template");
        File templateDir = new File(templateDirPath + "/template/");

        //定义输出
        File outputDir = MyFileUtils.makeDir(hardDisk, path+"webapp/view/js/" + className);
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputDir + "/" + fileName + ".js");
        template.process(root, out);
        out.flush();
        out.close();
    }

    public void generateWebJsp(Map<String, Object> root, String templateName, String fileName, String className) throws Exception {
//        File templateDir = MyFileUtils.makeDir("E:/", "project/branches/generator/src/generator/template");
        File templateDir = new File(templateDirPath + "/template/");

        //定义输出
        File outputDir = MyFileUtils.makeDir(hardDisk, path+"webapp/view/jsp/" + className);
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputDir + "/" + fileName + ".jsp");
        template.process(root, out);
        out.flush();
        out.close();
    }

    public void generateWebIndexJsp(Map<String, Object> root, String templateName) throws Exception {
//        File templateDir = MyFileUtils.makeDir("E:/", "project/branches/generator/src/generator/template");
        File templateDir = new File(templateDirPath + "/template/");

        //定义输出
        File outputDir = MyFileUtils.makeDir(hardDisk, path+"webapp/");
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputDir + "/" + "index" + ".jsp");
        template.process(root, out);
        out.flush();
        out.close();
    }


    public void generatePom(Map<String, Object> root, String templateName) throws Exception {
//        File templateDir = MyFileUtils.makeDir("E:/", "project/branches/generator/src/generator/template");
        File templateDir = new File(templateDirPath + "/template/");

        //定义输出
        File outputDir = MyFileUtils.makeDir(hardDisk, "generteProject/");
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputDir + "/" + "pom" + ".xml");
        template.process(root, out);
        out.flush();
        out.close();
    }

    public static void main(String[] args) throws Exception {
        Generator.process();

    }
}
